package com.wilmer.utils;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.wilmer.common.core.redis.RedisCache;
import com.wilmer.bean.TokenBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @author hans
 */
@Component
public class AccessTokenUtil {

    @Value("${wechat.token_url}")
    private  String access_token_url;
    @Value("${wechat.app_id}")
    private  String app_id;
    @Value("${wechat.app_secret}")
    private  String app_secret;
    @Value("${wechat.grant_type}")
    private  String grant_type;

    @Resource
     private   RedisCache redisCache;


    public  TokenBean getToken(){
        System.out.println(access_token_url+grant_type);
        //可以单独传入http参数，这样参数会自动做URL编码，拼接在URL中
        HashMap<String, Object> tokenMap = new HashMap<>();
        tokenMap.put("appid",app_id);
        tokenMap.put("secret",app_secret);
        tokenMap.put("grant_type",grant_type);
        String res = HttpUtil.get(access_token_url, tokenMap);
        //进行Json序列化
        TokenBean tokenBean = JSONUtil.toBean(res, TokenBean.class);
        return tokenBean;
    }
}



